﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.RA
{
    public partial class Researches : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;
        string insert;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[0].Selected = true;


            conn = new MySqlConnection(strProvider);
            conn.Open();

            // Get the id from querystring
            id = (string)Session["userName"];

            if (id == null)

                //id = "20000001";
                Response.Redirect("../login.aspx");

                string strSQL = " SELECT R.about " +
                            " FROM involved I, research R" +
                            " WHERE I.rid = R.rid AND I.ra_sid = " + id ;

                sqlCommand = new MySqlCommand(strSQL, conn);
                readVals = sqlCommand.ExecuteReader();

                while (readVals.Read())
                {
                    string tmp = readVals.GetString("about");
                    if(tmp.Length > 0)
                        if(tmp.IndexOf(" ") != -1)
                            Label2.Text = tmp.Substring(0, tmp.IndexOf(" "));
                        else
                            Label2.Text = tmp;
                }
                readVals.Close();
                conn.Close();
                Label1.Text = DropDownList1.SelectedValue;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            insert = DropDownList1.SelectedValue;
            Label1.Text = DropDownList1.SelectedValue;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            insert = DropDownList1.SelectedValue;
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string strSQL = " SELECT rid" +
                            " FROM research" +
                            " WHERE about = '" + insert+"'";

            sqlCommand = new MySqlCommand(strSQL, conn);
            readVals = sqlCommand.ExecuteReader();

            string rid = "";
            while (readVals.Read())
            {
              //  if (readVals.GetString("rid") != null)
                    rid = readVals.GetString("rid");
            }
            conn.Close();

            conn = new MySqlConnection(strProvider);
            conn.Open();

            string strSQL1 = "Update Involved" +
                " SET rid =" + rid +
              " WHERE ra_sid = " + id;
            sqlCommand = new MySqlCommand(strSQL1, conn);
            sqlCommand.ExecuteNonQuery();

            conn.Close();

            Response.Redirect("RA_Researches.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            
        }
    }
}